package com.example.demo.manager;

import com.example.demo.domain.po.Role;
import com.example.demo.domain.po.User;
import com.example.demo.domain.po.UserRole;
import com.example.demo.domain.vo.GroupVO;
import com.example.demo.service.RoleService;
import com.example.demo.service.UserRoleService;
import com.example.demo.service.UserService;
import org.activiti.engine.identity.Group;
import org.activiti.engine.impl.persistence.entity.GroupEntityManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


/**
 * @Description: 覆盖原本的GroupEntityManager实现自定义的角色
 * @Author: zsg
 * @Date: 2018/07/05 15:54
 */
@Service
public class CustomGroupEntityManager extends  GroupEntityManager {

    @Autowired
    private UserRoleService userRoleService;

    @Autowired
    private UserService userService;

    @Autowired
    private RoleService roleService;

    @Override
    public List<Group> findGroupsByUser(String userId) {
        User userByName = userService.findUserByName(userId);
        if (userByName==null){
            return super.findGroupsByUser(userId);
        }
        List<UserRole> userRoleByUid = this.userRoleService.findUserRoleByUid(userByName.getId());

        List<Group> groupList = new ArrayList<>();
        //获取用户对应的角色
        userRoleByUid.stream().forEach(userRole -> {
            Role role = this.roleService.findRoleById(userRole.getRid());
            GroupVO groupVO = new GroupVO();
            groupVO.setId(role.getRole());
            groupList.add(groupVO);
        });
        return groupList;
    }



}
